import {observable, action, toJS} from "mobx";
import StudentVerifyModel from '../models/StudentVerifyModel';
import QueryDeatilModel from '../models/QueryDeatilModel';

class QueryStore {
    api;
    appStore;
    @observable results = [];
    @observable accurateQuery = false;
    @observable positionQuery = false;
    @observable queryDetailModel = new QueryDeatilModel();
    @observable flag = -1;


    constructor(api, appStore) {
        this.api = api;
        this.appStore = appStore;
    }

    @action setAccurateQuery(type) {
        this.accurateQuery = type;
        this.queryDetailModel = new QueryDeatilModel();
    }

    @action setPositionQuery(type) {
        this.positionQuery = type;
        this.queryDetailModel = new QueryDeatilModel();
    }

    @action setModel(queryDeatilModel) {
        this.queryDetailModel = queryDeatilModel;
    }

    @action getModel() {
        return this.queryDetailModel;
    }

    @action setFlag(type){
        this.flag = type
    }

    @action setNewModel(){
        this.queryDetailModel = new QueryDeatilModel();
    }

    //精确查询
    @action fetchQueryByAccurate(detail) {
        this.appStore.increaseRequest();
        return this.api.queryAlumnuAccurateItem(detail).then(
            action(data => {
                this.appStore.decreaseRequest();
                if (data) {
                    this.results.clear();
                    this.flag=1;
                    this.results.push(StudentVerifyModel.fromJS(this, data));
                    return Promise.resolve();
                } else {
                    this.flag=0;
                    this.appStore.setError(data.error);
                    return Promise.reject();
                }
            })
        )
    }

    //位置查询
    @action fetchQueryByLocation(province, city) {
        this.appStore.increaseRequest();
        return this.api.queryAlumnuPositionList(province, city).then(
            action(data => {
                this.appStore.decreaseRequest();
                console.log("--------");
                console.log(data);
                console.log("--------");
                if (data) {
                    this.results.clear();
                    this.flag=1;
                    data.forEach(result => this.results.push(StudentVerifyModel.fromJS(this, result)));
                    return Promise.resolve();
                } else {
                    this.flag=0;
                    this.appStore.setError(data.error);
                    return Promise.reject();
                }
            })
        )
    }
}

export default QueryStore;